﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DictationTool.ViewModel;

namespace DictationTool.View
{
    /// <summary>
    /// Interaction logic for MainWindowView.xaml
    /// </summary>
    public partial class MainWindowView : UserControl
    {
        public MainWindowView()
        {
            LoadResources();
            InitializeComponent();
            NewTitle.MouseLeftButtonDown += HandleMouseLeftButtonDown;

            this.DataContext = mainWindow;
        }

        private static void LoadResources()
        {
            string path = @"..\Resources.xaml";
            var dictionaryResources = new ResourceDictionary { Source = new Uri(path, UriKind.Relative) };
            Application.Current.Resources.MergedDictionaries.Add(dictionaryResources);
        }

        private void HandleMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // Check for double click
            UIElement element = sender as UIElement;
            DateTime clickTime = DateTime.Now;
            TimeSpan span = clickTime - lastClick;

            if (span.TotalMilliseconds > 300 || firstClickDone == false)
            {
                firstClickDone = true;
                lastClick = DateTime.Now;
                clickPosition = e.GetPosition(element);
            }
            else
            {
                Point position = e.GetPosition(element);
                if (Math.Abs(clickPosition.X - position.X) < 4 && Math.Abs(clickPosition.Y - position.Y) < 4)
                {
                    mainWindow.MaximiceCommand.Execute(null);
                }
                firstClickDone = false;
            }

            Application.Current.MainWindow.DragMove();
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.F7:
                    mainWindow.MediaPlayerViewModel.PlayPauseCommand.Execute(null);
                    break;
                case Key.F8:
                    mainWindow.MediaPlayerViewModel.EndPointCommand.Execute(null);
                    break;
                default:
                    break;
            }
            base.OnKeyUp(e);
        }

        private DateTime lastClick = DateTime.Now;
        private bool firstClickDone = false;
        private Point clickPosition;
        private MainWindowViewModel mainWindow = new MainWindowViewModel();
    }
}
